package com.uplift.mgt.message.controller;

import com.uplift.common.Const;
import com.uplift.common.base.ServiceReslut;
import com.uplift.common.result.IResultable;
import com.uplift.common.result.ResultWrapper4Layui;
import com.uplift.mgt.building.model.RealtyFileModel;
import com.uplift.mgt.building.service.IRealtyFileService;
import com.uplift.mgt.message.model.MessageModel;
import com.uplift.mgt.message.service.IMessageService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 消息相关
 * 
 * @author bcwang
 *
 */
@RestController
@RequestMapping(value = "/message")
public class MessageController {
	public static Log log = LogFactory.getLog(MessageController.class);

	@Autowired
	private IMessageService messageService;

	@Autowired
	private IRealtyFileService realtyFileService;

	/**
	 * 查询消息信息
	 *
	 * @param request
	 * @param messageModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public IResultable list(HttpServletRequest request, MessageModel messageModel) {
		try {
			Map<?, ?> userMap = (Map<?, ?>) request.getSession().getAttribute(Const.SESSION_USER);

			if (userMap != null) {
				Object roleObj = userMap.get("role");
				if(StringUtils.equals(Objects.toString(roleObj), "4")) { 	//香港用户
					messageModel.setSourceCode("HK");
				}
			}

			List<Map<String, Object>> list = this.messageService.queryMessage(messageModel);
			return ResultWrapper4Layui.wrapListPage(list, ResultWrapper4Layui.SUCC, "查询成功");
		} catch (Exception e) {
			log.error(e);
			return ResultWrapper4Layui.wrapListPage(null, ResultWrapper4Layui.FAIL, "查询失败");
		}
	}

	/**
	 * 获取单个消息
	 *
	 * @param request
	 * @param messageModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/get-one")
	@ResponseBody
	public IResultable getOne(HttpServletRequest request, MessageModel messageModel) {
		try {
			List<Map<String, Object>> list = this.messageService.queryMessage(messageModel);
			if(list!=null && list.size()>0) {

				Map<String, Object> message = list.get(0);

				RealtyFileModel realtyFileModel = new RealtyFileModel();
				realtyFileModel.setMessageId(message.get("id").toString());
				message.put("files", realtyFileService.queryMessageRealtyFile(realtyFileModel));
				return ResultWrapper4Layui.wrapMap(list.get(0), ResultWrapper4Layui.SUCC, "查询成功");
			}

			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "查询成功");
		} catch (Exception e) {
			log.error(e);
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "查询失败");
		}
	}

	/**
	 * 新增消息记录
	 *
	 * @param request
	 * @param messageModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/add")
	public IResultable save(HttpServletRequest request, MessageModel messageModel) {
		try {
			ServiceReslut serviceReslut = this.messageService.addMessage(messageModel);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "保存成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败");
		}
	}

	/**
	 * 删除消息记录
	 *
	 * @param request
	 * @param messageModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/remove")
	public IResultable remove(HttpServletRequest request, MessageModel messageModel) {
		try {
			ServiceReslut serviceReslut = this.messageService.deleteMessage(messageModel);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "删除成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败");
		}
	}

	/**
	 * 更新消息记录
	 * @param request
	 * @param messageModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/update")
	public IResultable update(HttpServletRequest request, MessageModel messageModel) {
		try {
			ServiceReslut serviceReslut = this.messageService.updateMessage(messageModel);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "更新成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "更新失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "更新失败");
		}
	}

	/**
	 * 发送消息
	 * @param request
	 * @param messageModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/send-message")
	public IResultable send(HttpServletRequest request, MessageModel messageModel) {
		try {
			ServiceReslut serviceReslut = this.messageService.sendMessage(messageModel);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "发送成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "发送失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "发送失败");
		}
	}

	/**
	 * 撤回消息
	 * @param request
	 * @param messageModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/reback-message")
	public IResultable reback(HttpServletRequest request, MessageModel messageModel) {
		try {
			ServiceReslut serviceReslut = this.messageService.rebackMessage(messageModel);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "撤回成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "撤回失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "撤回失败");
		}
	}
}
